const express = require('express');
const router = express.Router();
const adminController = require('../controllers/adminController');
const { verifyToken, isAdmin } = require('../middlewares/auth');

// 所有路由都需要验证令牌和管理员权限
router.use(verifyToken, isAdmin);

/**
 * @swagger
 * /api/admin/users:
 *   get:
 *     summary: 获取所有用户
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取用户列表
 *       401:
 *         description: 未授权
 *       403:
 *         description: 需要管理员权限
 */
router.get('/users', adminController.getUsers);

/**
 * @swagger
 * /api/admin/users:
 *   post:
 *     summary: 创建用户
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *               - name
 *             properties:
 *               username:
 *                 type: string
 *               password:
 *                 type: string
 *               name:
 *                 type: string
 *               role:
 *                 type: string
 *                 enum: [admin, user]
 *     responses:
 *       201:
 *         description: 用户创建成功
 *       400:
 *         description: 用户名已存在
 */
router.post('/users', adminController.createUser);

/**
 * @swagger
 * /api/admin/users/{id}:
 *   put:
 *     summary: 更新用户
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               role:
 *                 type: string
 *                 enum: [admin, user]
 *     responses:
 *       200:
 *         description: 用户更新成功
 *       404:
 *         description: 用户不存在
 */
router.put('/users/:id', adminController.updateUser);

/**
 * @swagger
 * /api/admin/users/{id}:
 *   delete:
 *     summary: 删除用户
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 用户删除成功
 *       400:
 *         description: 不能删除当前登录的用户
 *       404:
 *         description: 用户不存在
 */
router.delete('/users/:id', adminController.deleteUser);

/**
 * @swagger
 * /api/admin/invitations:
 *   get:
 *     summary: 获取所有邀请链接
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取邀请链接列表
 */
router.get('/invitations', adminController.getInvitations);

/**
 * @swagger
 * /api/admin/invitations:
 *   post:
 *     summary: 创建邀请链接
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - position
 *             properties:
 *               title:
 *                 type: string
 *               description:
 *                 type: string
 *               position:
 *                 type: string
 *               department:
 *                 type: string
 *               expiresAt:
 *                 type: string
 *                 format: date-time
 *               maxSubmissions:
 *                 type: integer
 *     responses:
 *       201:
 *         description: 邀请链接创建成功
 */
router.post('/invitations', adminController.createInvitation);

/**
 * @swagger
 * /api/admin/invitations/{id}:
 *   get:
 *     summary: 获取单个邀请链接
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取邀请链接
 *       404:
 *         description: 邀请链接不存在
 */
router.get('/invitations/:id', adminController.getInvitation);

/**
 * @swagger
 * /api/admin/invitations/{id}:
 *   put:
 *     summary: 更新邀请链接
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *               description:
 *                 type: string
 *               position:
 *                 type: string
 *               department:
 *                 type: string
 *               expiresAt:
 *                 type: string
 *                 format: date-time
 *               isActive:
 *                 type: boolean
 *               maxSubmissions:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 邀请链接更新成功
 *       404:
 *         description: 邀请链接不存在
 */
router.put('/invitations/:id', adminController.updateInvitation);

/**
 * @swagger
 * /api/admin/invitations/{id}:
 *   delete:
 *     summary: 删除邀请链接
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 邀请链接删除成功
 *       404:
 *         description: 邀请链接不存在
 */
router.delete('/invitations/:id', adminController.deleteInvitation);

/**
 * @swagger
 * /api/admin/resumes/recent:
 *   get:
 *     summary: 获取最近简历
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取最近简历
 */
router.get('/resumes/recent', adminController.getRecentApplicants);

/**
 * @swagger
 * /api/admin/resumes/export:
 *   get:
 *     summary: 导出简历
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [待审核, 已通过, 已拒绝]
 *       - in: query
 *         name: invitationId
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功导出简历
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: 没有找到符合条件的简历
 */
router.get('/resumes/export', adminController.exportResumes);

/**
 * @swagger
 * /api/admin/resumes:
 *   get:
 *     summary: 获取所有简历
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [待审核, 已通过, 已拒绝]
 *       - in: query
 *         name: invitationId
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取简历列表
 */
router.get('/resumes', adminController.getResumes);

/**
 * @swagger
 * /api/admin/resumes/{id}:
 *   get:
 *     summary: 获取单个简历
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取简历
 *       404:
 *         description: 简历不存在
 */
router.get('/resumes/:id', adminController.getResume);

/**
 * @swagger
 * /api/admin/resumes/{id}/status:
 *   put:
 *     summary: 更新简历状态
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [待审核, 已通过, 已拒绝]
 *               remark:
 *                 type: string
 *     responses:
 *       200:
 *         description: 简历状态更新成功
 *       404:
 *         description: 简历不存在
 */
router.put('/resumes/:id/status', adminController.updateResumeStatus);

/**
 * @swagger
 * /api/admin/resumes/{id}:
 *   delete:
 *     summary: 删除简历
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 简历删除成功
 *       404:
 *         description: 简历不存在
 */
router.delete('/resumes/:id', adminController.deleteResume);

/**
 * @swagger
 * /api/admin/statistics:
 *   get:
 *     summary: 获取统计数据
 *     tags: [管理员]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取统计数据
 */
router.get('/statistics', adminController.getStatistics);

module.exports = router; 